using System;
using System.Configuration;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Slf.Config;
using Slf.Factories;

namespace SLF.Test.Config
{
  /// <summary>
  /// 
  /// </summary>
  [TestClass]
  public class Given_SlfConfigurationSection_When_Parsing_ConfigurationFile
  {
    /// <summary>
    /// Simple positive test, ensure that the factory name and type are properly mapped 
    /// from the configuration XML file.
    /// </summary>
    [TestMethod]
    [DeploymentItem("TestData/Configurations/VariousDeclarations.exe")]
    [DeploymentItem("TestData/Configurations/VariousDeclarations.exe.config")]
    public void Section_Should_Contain_Declared_Factories()
    {
      Configuration config = ConfigurationManager.OpenExeConfiguration("VariousDeclarations.exe");
      SlfConfigurationSection slfConfig = (SlfConfigurationSection)config.GetSection("slf");

      Assert.AreEqual(5, slfConfig.Factories.Count);

      Assert.AreEqual(String.Empty, slfConfig.Factories[0].Name);
      Assert.AreEqual("traceFactory1", slfConfig.Factories[1].Name);
      Assert.AreEqual("traceFactory2", slfConfig.Factories[2].Name);
      Assert.AreEqual("debugFactory1", slfConfig.Factories[3].Name);
      Assert.AreEqual("debugFactory2", slfConfig.Factories[4].Name);

      Assert.AreEqual(typeof(ConsoleLoggerFactory), ResolveType(slfConfig.Factories[0]));
      Assert.AreEqual(typeof(TraceLoggerFactory), ResolveType(slfConfig.Factories[1]));
      Assert.AreEqual(typeof(TraceLoggerFactory), ResolveType(slfConfig.Factories[2]));
      Assert.AreEqual(typeof(DebugLoggerFactory), ResolveType(slfConfig.Factories[3]));
      Assert.AreEqual(typeof(DebugLoggerFactory), ResolveType(slfConfig.Factories[4]));
    }


    private static Type ResolveType(FactoryConfigurationElement element)
    {
      string typeName = element.Type;
      return typeName == null ? null : Type.GetType(typeName); 
    }


    /// <summary>
    /// Simple positive test, ensure that the factory name and type are properly mapped 
    /// from the configuration XML file.
    /// </summary>
    [TestMethod]
    [DeploymentItem("TestData/Configurations/VariousDeclarations.exe")]
    [DeploymentItem("TestData/Configurations/VariousDeclarations.exe.config")]
    public void Section_Should_Contain_Declared_Loggers()
    {
      Configuration config = ConfigurationManager.OpenExeConfiguration("VariousDeclarations.exe");
      SlfConfigurationSection slfConfig = (SlfConfigurationSection)config.GetSection("slf");


      Assert.AreEqual(7, slfConfig.Loggers.Count);

      Assert.AreEqual(String.Empty, slfConfig.Loggers[0].LoggerName);
      Assert.AreEqual("consoleLogger", slfConfig.Loggers[1].LoggerName);
      Assert.AreEqual("traceLogger1", slfConfig.Loggers[2].LoggerName);
      Assert.AreEqual("traceLogger2.1", slfConfig.Loggers[3].LoggerName);
      Assert.AreEqual("traceLogger2.2", slfConfig.Loggers[4].LoggerName);
      Assert.AreEqual("debugLogger1", slfConfig.Loggers[5].LoggerName);
      Assert.AreEqual("debugLogger2", slfConfig.Loggers[6].LoggerName);

      Assert.AreEqual(slfConfig.Factories[0].Name, slfConfig.Loggers[0].FactoryName);
      Assert.AreEqual(slfConfig.Factories[0].Name, slfConfig.Loggers[1].FactoryName);
      Assert.AreEqual(slfConfig.Factories[1].Name, slfConfig.Loggers[2].FactoryName);
      Assert.AreEqual(slfConfig.Factories[2].Name, slfConfig.Loggers[3].FactoryName);
      Assert.AreEqual(slfConfig.Factories[2].Name, slfConfig.Loggers[4].FactoryName);
      Assert.AreEqual(slfConfig.Factories[3].Name, slfConfig.Loggers[5].FactoryName);
      Assert.AreEqual(slfConfig.Factories[4].Name, slfConfig.Loggers[6].FactoryName);

    }


    //TODO test implicit retrieval of default loggers / factories
    //TODO test functionality of implicit names / missing types

  }
}
